package com.infra.server.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.infra.server.api.Result;
import com.infra.server.entity.SysRouter;
import com.infra.server.service.SysRouterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author win
 * @date 2020/12/21 10:49
 */
@RestController
@Api(value = "SysRouterController",tags = "路由相关接口")
@RequestMapping("/router")
public class SysRouterController {

    @Resource
    private SysRouterService sysRouterService;

    @ApiOperation(value = "获取全部路由列表,且包含树结构。传分页参数则返回分页数据",notes = "1")
    @GetMapping("/list")
    public Result getRouters(@RequestParam(required = false) Integer current, @RequestParam(required = false) Integer size) {
        Map<String,Object> map = new HashMap<>();
        if (current != null && size != null) {
            // 分页查询
            Page<SysRouter> page = new Page<>(current,size);
            IPage<SysRouter> page1 = sysRouterService.page(page);
            map.put("page",page1);
        } else {
            // 获取所有路由
            List<SysRouter> list = sysRouterService.list();
            map.put("list",list);

            // 构造路由树
            List<Tree<String>> tree = getRouterTree(list);
            map.put("tree",tree);
        }
        return Result.ok().data(map).message("获取路由成功");
    }

    /**
     * hutool树工具构建路由树
     */
    public List<Tree<String>> getRouterTree(List<SysRouter> sysRouterList) {
        // 自定义属性名，详见Hutool树工具
        // https://hutool.cn/docs/#/core/%E8%AF%AD%E8%A8%80%E7%89%B9%E6%80%A7/%E6%A0%91%E7%BB%93%E6%9E%84/%E6%A0%91%E7%BB%93%E6%9E%84%E5%B7%A5%E5%85%B7-TreeUtil
//        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
//        treeNodeConfig.setParentIdKey("pid");
        List<Tree<String>> routers = TreeUtil.build(sysRouterList, "0",
                (router, tree) -> {
                    tree.setId(router.getId().toString());
                    tree.setParentId(router.getParentId().toString());
                    // 扩展属性
                    if (!StrUtil.hasEmpty(router.getTitle())) {
                        tree.putExtra("label",router.getTitle());
                    } else {
                        tree.putExtra("label","(无标题)");
                    }
                });
        return routers;
    }

    @ApiOperation(value = "保存或修改路由",notes = "1")
    @PostMapping("/save")
    public Result saveOrUpdateRouter(@RequestBody SysRouter sysRouter) {
        boolean a = sysRouterService.saveOrUpdate(sysRouter);
        if (a) {
            return Result.ok().message("添加或修改路由成功");
        } else {
            return Result.error().message("添加或修改路由失败，请重试！");
        }
    }

    @ApiOperation(value = "根据id删除路由",notes = "1")
    @GetMapping("/delete")
    public Result deleteRouter(@RequestParam Integer id) {
        boolean a = sysRouterService.delRouterByRouterId(id);
        if (a) {
            return Result.ok().message("删除路由成功");
        } else {
            return Result.error().message("删除路由失败，请重试！");
        }
    }

}
